package com.mi.cmdtest;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

/**
 * 搜索到的设备adapter
 * Created by Administrator on 2016/10/12.
 */

public class DevicesSearchedAdapter extends RecyclerView.Adapter<DevicesSearchedAdapter.DeviceHolder> {
    private Context mContext;
    private LayoutInflater inflater;
    private OnDeviceClickListener onDeviceClickListener;
    private List<DeviceSearch> data;


    public void setOnDeviceClickListener(OnDeviceClickListener onDeviceClickListener) {
        this.onDeviceClickListener = onDeviceClickListener;
    }

    public DevicesSearchedAdapter(Context mContext, List<DeviceSearch> data) {
        this.mContext = mContext;
        this.inflater = LayoutInflater.from(mContext);
        this.data = data;
    }

    @Override
    public DevicesSearchedAdapter.DeviceHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DeviceHolder(inflater.inflate(R.layout.item_recycler_device, parent, false));
    }

    @Override
    public void onBindViewHolder(DevicesSearchedAdapter.DeviceHolder holder, final int position) {
        holder.layout_device_item.setBackgroundResource(R.drawable.device_bg_selector);
        holder.tv_device_item_name.setText(data.get(position).getBluetoothDevice().getName() + data.get(position).getBle_name()+"  "+data.get(position).getRssi());
        if (data.get(position).isSelected()){
            holder.iv_device_selected_point.setVisibility(View.VISIBLE);
        }else {
            holder.iv_device_selected_point.setVisibility(View.GONE);
        }
        holder.layout_device_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onDeviceClickListener != null) {
                    onDeviceClickListener.onDeviceClick(data.get(position));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }


    public class DeviceHolder extends RecyclerView.ViewHolder {
        TextView tv_device_item_name;
        RelativeLayout layout_device_item;
        ImageView iv_device_selected_point;
        public DeviceHolder(View itemView) {
            super(itemView);
            layout_device_item = (RelativeLayout) itemView.findViewById(R.id.layout_device_item);
            tv_device_item_name = (TextView) itemView.findViewById(R.id.tv_device_item_name);
            iv_device_selected_point = (ImageView) itemView.findViewById(R.id.iv_device_selected_point);
        }
    }

    public interface OnDeviceClickListener {
        void onDeviceClick(DeviceSearch device);
    }
}
